package com.gopay.bis.hxbc.ebank.operation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.hxbc.ebank.HXBCMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelperHxbc;

/**
 * 华夏签名验签实现类
 * 
 * @HxbcB2cCertHelper.java
 * @author LiLiLi
 */
public class HxbcB2cCertHelper implements ICertHelperHxbc {
	/** socket实现类 */
	private SocketTransport4HXBC socketTransport4HXBC;
	private static Logger logger = LoggerFactory
			.getLogger(HxbcB2cCertHelper.class);

	/**
	 * 向前置机发送签名
	 */
	@Override
	public String sign(BaseMessage baseMessage) throws GopayException {
		try {
			HXBCMessage hxbcMessage = (HXBCMessage) baseMessage;
			String plaintxt = hxbcMessage.getPlainMessage();
			String signMessage = (String) socketTransport4HXBC
					.submitSignReq(plaintxt);
			return signMessage;
		} catch (Exception e) {
			BankLogUtils.printB2CSignExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
		}
	}

	/**
	 * 验签
	 */
	@Override
	public Object verify(BaseMessage baseMessage) throws GopayException {
		try {
			HXBCMessage hxbcMessage = (HXBCMessage) baseMessage;
			String signMessage = hxbcMessage.getSignMessage();
			String plaintxt = (String) socketTransport4HXBC
					.submitverify(signMessage);
			return plaintxt;
		} catch (Exception e) {
			BankLogUtils.printB2CVerifyExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
		}
	}

	/**
	 * 向银行发送签名
	 * 
	 * @param param
	 * @return
	 */
	@Override
	public String signToBank(BaseMessage baseMessage) throws GopayException {
		try {
			HXBCMessage hxbcMessage = (HXBCMessage) baseMessage;
			String signMessage = hxbcMessage.getSignMessage();
			String signMsg = (String) socketTransport4HXBC
					.submitSignReqSecond(signMessage);
			return signMsg;
		} catch (Exception e) {
			BankLogUtils.printB2CSignExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
		}
	}

	public void setSocketTransport4HXBC(
			SocketTransport4HXBC socketTransport4HXBC) {
		this.socketTransport4HXBC = socketTransport4HXBC;
	}

}
